﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zen4SyncPOCO.POCO.Admin;
using Zen4SyncPOCO.POCO.Scenario;
using Zen4SyncPOCO.POCO.MergeSync;
using Zen4SyncPOCO.POCO.SQLServerExecution;
using Zen4SyncPOCO.POCO.SQLCEExecution;
using Zen4SyncPOCO.POCO.SQLCEBackup;
using Zen4SyncPOCO.POCO.MEF;
using Zen4SyncPOCO.Enum.MergeSync;
using Zen4SyncPOCO.Enum.Admin;
using Zen4SyncPOCO.POCO.Enum.Scenario;
using Zen4SyncModel;
using Zen4SyncModel.Exceptions;
using Zen4SyncOrchestrator.Exceptions;
using System.Data.Objects.DataClasses;

namespace Zen4SyncOrchestrator
{
    internal class ModelConverter
    {
        IEntityProvider z4sEntityProvider;

        public ModelConverter()
        {
            z4sEntityProvider = EntityProviderFactory.Instance;
        }

        /// <summary>
        /// Converts a Zen4Sync clientSession into a POCO ClientSession.
        /// This POCO ClientSession ONLY contains what is needed by a Test Server to run the clientSession.
        /// </summary>
        /// <param name="z4sClientSession">Zen4Sync clientSession</param>
        /// <returns>The POCO ClientSession of the corresponding Zen4Sync clientSession</returns>
        internal ClientSession ConvertToPOCO( clientSession z4sClientSession, string zen4SyncSymmetricKeyPassword )
        {
            ClientSession clientSession = new ClientSession()
            {
                clientSession_id = z4sClientSession.clientSession_id,
                clientSession_testServerId = z4sClientSession.clientSession_testServerId,
                clientSession_testSessionId = z4sClientSession.clientSession_testSessionId,
                clientSession_enableLog = z4sClientSession.clientSession_enableLog,
                clientSession_scheduledStartDateTime = z4sClientSession.clientSession_scheduledStartDateTime,
                clientSession_testScenario = ConvertToPOCO( z4sClientSession.testScenario, zen4SyncSymmetricKeyPassword )
            };
            clientSession.clientSession_activityContexts = ConvertToPOCO( z4sClientSession.activityContext, clientSession.clientSession_testScenario.testScenario_activities, zen4SyncSymmetricKeyPassword );

            return clientSession;
        }

        /// <summary>
        /// Converts a Zen4Sync testScenario into a POCO TestScenario
        /// </summary>
        /// <param name="z4sTestScenario">Zen4Sync testScenario</param>
        /// <returns>The POCO TestScenario of the corresponding Zen4Sync testScenario</returns>
        private TestScenario ConvertToPOCO( testScenario z4sTestScenario, string zen4SyncSymmetricKeyPassword )
        {
            return new TestScenario()
            {
                testScenario_id = z4sTestScenario.testScenario_id,
                testScenario_activities = ConvertToPOCO( z4sTestScenario.activity, zen4SyncSymmetricKeyPassword )

            };
        }

        /// <summary>
        /// Converts a Zen4Sync activity into a POCO Activity
        /// </summary>
        /// <param name="z4sActivity">Zen4Sync activity</param>
        /// <returns>The POCO Activity of the corresponding Zen4Sync activity</returns>
        private Activity ConvertToPOCO( activity z4sActivity, string zen4SyncSymmetricKeyPassword )
        {
            Activity activity = new Activity()
            {
                activity_id = z4sActivity.activity_id,
                activity_executionOrder = z4sActivity.activity_executionOrder,
                activity_preWaitingTimeInSeconds = z4sActivity.activity_preWaitingTimeInSeconds,
                activity_postWaitingTimeInSeconds = z4sActivity.activity_postWaitingTimeInSeconds,
                activity_activityTypeEnum = GuidToEnum.ActivityType(z4sActivity.activity_activityTypeId)
            };

            switch (activity.activity_activityTypeEnum)
            {
                case ActivityTypeEnum.SyncSession:
                    activity.activity_syncSession = POCOSyncSession(z4sActivity.activity_activityItemId, zen4SyncSymmetricKeyPassword);
                    break;
                case ActivityTypeEnum.SqlServerExecution:
                    activity.activity_sqlServerExecution = POCOSqlServerExecution(z4sActivity.activity_activityItemId);
                    break;
                case ActivityTypeEnum.SqlServerCheckData:
                    activity.activity_sqlServerCheckData = POCOSqlServerCheckData(z4sActivity.activity_activityItemId);
                    break;
                case ActivityTypeEnum.SqlCeExecution:
                    activity.activity_sqlCeExecution = POCOSqlceExecution(z4sActivity.activity_activityItemId);
                    break;
                case ActivityTypeEnum.SqlCeCheckData:
                    activity.activity_sqlCeCheckData = POCOSqlceCheckData(z4sActivity.activity_activityItemId);
                    break;
                case ActivityTypeEnum.SqlCeBackup:
                    activity.activity_sqlCeBackup = POCOSqlceBackup(z4sActivity.activity_activityItemId);
                    break;
                case ActivityTypeEnum.MefExecution:
                    activity.activity_mefExecution = POCOMefExecution(z4sActivity.activity_activityItemId);
                    break;
                default:
                    // Can't happen since an exception has been thrown earlier by GuidToEnum.ActivityType() if the activityType does not match any valid activityType
                    break;
            }
            

            return activity;
        }


        /// <summary>
        /// Gets the syncSession entity from his unique identifier.
        /// Converts this entity in a POCO SyncSession.
        /// </summary>
        /// <param name="syncSessionId">Unique identifier of the Zen4Sync syncSession</param>
        /// <returns>A POCO SyncSession</returns>
        private SyncSession POCOSyncSession( Guid syncSessionId, string zen4SyncSymmetricKeyPassword )
        {
            syncSession z4sSyncSession = z4sEntityProvider.GetSyncSession( syncSessionId );
            SyncSession syncSession = new SyncSession()
            {
                syncSession_id = z4sSyncSession.syncSession_id,
                syncSession_compressionLevel = z4sSyncSession.syncSession_compressionLevel,
                syncSession_connectionRetryTimeout = z4sSyncSession.syncSession_connectionRetryTimeout,
                syncSession_connectTimeout = z4sSyncSession.syncSession_connectTimeout,
                syncSession_loginTimeout = z4sSyncSession.syncSession_loginTimeout,
                syncSession_postSyncCleanup = z4sSyncSession.syncSession_postSyncCleanup,
                syncSession_publication = z4sSyncSession.syncSession_publication,
                syncSession_queryTimeout = z4sSyncSession.syncSession_queryTimeout,
                syncSession_receiveTimeout = z4sSyncSession.syncSession_receiveTimeout,
                syncSession_sendTimeout = z4sSyncSession.syncSession_sendTimeout,
                syncSession_publisher = ConvertToPOCO( z4sSyncSession.publisher, zen4SyncSymmetricKeyPassword ),
                syncSession_distributor = ConvertToPOCO( z4sSyncSession.distributor, zen4SyncSymmetricKeyPassword ),
                syncSession_internet = ConvertToPOCO( z4sSyncSession.internet, zen4SyncSymmetricKeyPassword ),
                syncSession_hostName = !String.IsNullOrEmpty(z4sSyncSession.syncSession_hostName) ? z4sSyncSession.syncSession_hostName : null,
                syncSession_exchangeTypeEnum = GuidToEnum.ExchangeType(z4sSyncSession.syncSession_exchangeTypeId),
                syncSession_snapshotTransferTypeEnum = GuidToEnum.SnapshotTransferType(z4sSyncSession.syncSession_snapshotTransferTypeId)
            };

            return syncSession;
        }

        /// <summary>
        /// Gets the sqlServerExecution entity from his unique identifier.
        /// Converts this entity in a POCO SqlServerExecution
        /// </summary>
        /// <param name="sqlServerExecutionId">Unique identifier of the Zen4Sync sqlServerExecution</param>
        /// <returns>A POCO SqlServerExecution </returns>
        private SqlServerExecution POCOSqlServerExecution( Guid sqlServerExecutionId )
        {
            sqlServerExecution z4sSqlServerExecution = z4sEntityProvider.GetSqlServerExecution( sqlServerExecutionId );
            return new SqlServerExecution()
            {
                sqlServerExecution_id = z4sSqlServerExecution.sqlServerExecution_id,
                sqlServerExecution_sqlQuery = z4sSqlServerExecution.sqlServerExecution_sqlQuery
            };
        }

        /// <summary>
        /// Gets the sqlServerCheckData entity from his unique identifier.
        /// Converts this entity in a POCO SqlServerCheckData.
        /// </summary>
        /// <param name="sqlServerCheckDataId">Unique identifier of the Zen4Sync sqlServerCheckData</param>
        /// <returns>A POCO SqlServerCheckData</returns>
        private SqlServerCheckData POCOSqlServerCheckData( Guid sqlServerCheckDataId )
        {
            sqlServerCheckData z4sSqlServerCheckData = z4sEntityProvider.GetSqlServerCheckData( sqlServerCheckDataId );
            SqlServerCheckData sqlServerCD = new SqlServerCheckData()
            {
                sqlServerCheckData_id = z4sSqlServerCheckData.sqlServerCheckData_id,
                sqlServerCheckData_sqlQuery = z4sSqlServerCheckData.sqlServerCheckData_sqlQuery,
                sqlServerCheckData_expectedValue = z4sSqlServerCheckData.sqlServerCheckData_expectedValue,
                sqlServerCheckData_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlServerCheckData.sqlServerCheckData_sqlValueTypeId)
            };

            return sqlServerCD;
        }

        /// <summary>
        /// Gets the sqlceExecution entity from his unique identifier.
        /// Converts this entity in a POCO SqlceExecution
        /// </summary>
        /// <param name="sqlceExecutionId">Unique identifier of the Zen4Sync sqlceExecution</param>
        /// <returns>A POCO SqlceExecution</returns>
        private SqlceExecution POCOSqlceExecution( Guid sqlceExecutionId )
        {
            sqlceExecution z4sSqlceExecution = z4sEntityProvider.GetSqlceExecution( sqlceExecutionId );
            return new SqlceExecution()
            {
                sqlceExecution_id = z4sSqlceExecution.sqlceExecution_id,
                sqlceExecution_sqlQuery = z4sSqlceExecution.sqlceExecution_sqlQuery
            };
        }

        /// <summary>
        /// Gets the sqlceCheckData entity from his unique identifier.
        /// Converts this entity in a POCO SqlceCheckData.
        /// </summary>
        /// <param name="sqlceCheckDataId">Unique identifier of the Zen4Sync sqlceCheckData</param>
        /// <returns>A POCO SqlceCheckData</returns>
        private SqlceCheckData POCOSqlceCheckData( Guid sqlceCheckDataId )
        {
            sqlceCheckData z4sSqlceCheckData = z4sEntityProvider.GetSqlceCheckData( sqlceCheckDataId );
            SqlceCheckData sqlceCheckData = new SqlceCheckData()
            {
                sqlceCheckData_id = z4sSqlceCheckData.sqlceCheckData_id,
                sqlceCheckData_sqlQuery = z4sSqlceCheckData.sqlceCheckData_sqlQuery,
                sqlceCheckData_expectedValue = z4sSqlceCheckData.sqlceCheckData_expectedValue,
                sqlceCheckData_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlceCheckData.sqlceCheckData_sqlValueTypeId)
            };
            
            return sqlceCheckData;
        }

        /// <summary>
        /// Gets the sqlceBackup entity from his unique identifier.
        /// Converts this entity into a POCO SqlceBackup
        /// </summary>
        /// <param name="sqlceBackupId">Unique identifier of the Zen4Sync sqlceBackup</param>
        /// <returns>A POCO SqlceBackup</returns>
        private SqlceBackup POCOSqlceBackup( Guid sqlceBackupId )
        {
            sqlceBackup z4sSqlceBackup = z4sEntityProvider.GetSqlceBackup( sqlceBackupId );
            return new SqlceBackup()
            {
                sqlceBackup_id = z4sSqlceBackup.sqlceBackup_id
            };
        }
      
        /// <summary>
        /// Gets the mefExecution entity from his unique identifier.
        /// Converts this entity into a POCO SqlceBackup
        /// </summary>
        /// <param name="mefExecutionId"></param>
        /// <returns></returns>
        private MefExecution POCOMefExecution( Guid mefExecutionId )
        {
            mefExecution z4sMefExecution = z4sEntityProvider.GetMefExecution( mefExecutionId );
            return new MefExecution()
            {
                mefExecution_id = z4sMefExecution.mefExecution_id,
                mefExecution_metadata = z4sMefExecution.mefExecution_metadata,
                mefExecution_mefComponentDirectory = z4sEntityProvider.GetMefDeploymentDirectory(mefExecutionId)
            };
        }

        /// <summary>
        /// Returns all the POCO ActivityContext corresponding to a ClientSession 
        /// </summary>
        /// <param name="z4sContextMetadatas">A list of Zen4Sync activityContext</param>
        /// <param name="ClientSession">The ClientSession corresponding to the list of ActivityContext</param>
        /// <returns>A list of POCO ActivityContext corresponding to a ClientSession</returns>
        private List<ActivityContext> ConvertToPOCO( EntityCollection<activityContext> z4sActivityContexts, List<Activity> activities, string zen4SyncSymmetricKeyPassword )
        {
            List<ActivityContext> activityContexts = new List<ActivityContext>();
            foreach ( activityContext z4sActivityContext in z4sActivityContexts )
            {
                ActivityContext activityContext = new ActivityContext()
                {
                    activityContext_id = z4sActivityContext.activityContext_id,
                    activityContext_activityId = z4sActivityContext.activityContext_activityId,
                    activityContext_overridingData = z4sActivityContext.activityContext_overridingData
                };

                Activity activityOfContext = activities.Where(act => act.activity_id == z4sActivityContext.activityContext_activityId).SingleOrDefault();

                switch (activityOfContext.activity_activityTypeEnum)
                {
                    case ActivityTypeEnum.SyncSession:
                        activityContext.activityContext_syncSessionContext = POCOSyncSessionContext(z4sActivityContext.activityContext_contextItemId);
                        break;
                    case ActivityTypeEnum.SqlServerExecution:
                        activityContext.activityContext_sqlServerExecutionContext = POCOsqlServerExecutionContext(z4sActivityContext.activityContext_contextItemId, zen4SyncSymmetricKeyPassword);
                        break;
                    case ActivityTypeEnum.SqlServerCheckData:
                        activityContext.activityContext_sqlServerCheckDataContext = POCOsqlServerCheckDataContext(z4sActivityContext.activityContext_contextItemId, zen4SyncSymmetricKeyPassword);
                        break;
                    case ActivityTypeEnum.SqlCeExecution:
                        activityContext.activityContext_sqlCeExecutionContext = POCOsqlCeExecutionContext( z4sActivityContext.activityContext_contextItemId, zen4SyncSymmetricKeyPassword );
                        break;
                    case ActivityTypeEnum.SqlCeCheckData:
                        activityContext.activityContext_sqlCeCheckDataContext = POCOsqlCeCheckDataContext( z4sActivityContext.activityContext_contextItemId, zen4SyncSymmetricKeyPassword );
                        break;
                    case ActivityTypeEnum.SqlCeBackup:
                        activityContext.activityContext_sqlCeBackupContext = POCOsqlCeBackupContext( z4sActivityContext.activityContext_contextItemId );
                        break;
                    case ActivityTypeEnum.MefExecution:
                        activityContext.activityContext_mefExecutionContext = POCOMefExecutionContext( z4sActivityContext.activityContext_contextItemId );
                        break;
                    default:
                        throw new Zen4SyncOrchestratorException( Zen4SyncOrchestratorException.Messages.UNKNOWN_ITEM_BY_ID, new string[] { "ctivityTypeEnum", activityOfContext.activity_activityTypeEnum.ToString() } );
                }

                activityContexts.Add( activityContext );
            }
            return activityContexts;
        }

        private SyncSessionContext POCOSyncSessionContext( Guid syncSessionContextId )
        {
            syncSessionContext z4sSyncSessionContext = z4sEntityProvider.GetSyncSessionContext( syncSessionContextId );
            return new SyncSessionContext()
            {
                syncSessionContext_id = z4sSyncSessionContext.syncSessionContext_id,
                syncSessionContext_subscriber = ConvertToPOCO( z4sSyncSessionContext.subscriber )
            };

        }

        private MefExecutionContext POCOMefExecutionContext( Guid mefExecutionContextId )
        {
            mefExecutionContext z4sMefExecutionContext = z4sEntityProvider.GetMefExecutionContext( mefExecutionContextId );
            return new MefExecutionContext()
            {
                mefExecutionContext_id = z4sMefExecutionContext.mefExecutionContext_id,
                mefExecutionContext_metadata = z4sMefExecutionContext.mefExecutionContext_metadata
            };
        }

        private SqlceBackupContext POCOsqlCeBackupContext( Guid sqlceBackupContextId )
        {
            sqlceBackupContext z4sSqlceBackupContext = z4sEntityProvider.GetSqlceBackupContext( sqlceBackupContextId );
            return new SqlceBackupContext()
            {
                sqlceBackupContext_id = z4sSqlceBackupContext.sqlceBackupContext_id,
                sqlceBackupContext_sdfFileName = z4sSqlceBackupContext.sqlceBackupContext_sdfFileName,
                sqlceBackupContext_sdfFileDirectory = z4sSqlceBackupContext.sqlceBackupContext_sdfFileDirectory
            };
        }

        private SqlceCheckDataContext POCOsqlCeCheckDataContext( Guid sqlceCheckDataContextId, string zen4SyncSymmetricKeyPassword )
        {
            sqlceCheckDataContext z4sSqlceCheckDataContext = z4sEntityProvider.GetSqlceCheckDataContext( sqlceCheckDataContextId );
            SqlceCheckDataContext sqlceCDC = new SqlceCheckDataContext()
            {
                sqlceCheckDataContext_id = z4sSqlceCheckDataContext.sqlceCheckDataContext_id,
                sqlceCheckDataContext_parameters = ConvertToPOCO( z4sSqlceCheckDataContext.sqlceCheckDataParameter )
            };

            if(z4sSqlceCheckDataContext.sqlceCheckDataContext_isEncryptedConnectionString)
            {
                sqlceCDC.sqlceCheckDataContext_connectionString = z4sEntityProvider.DecryptData( z4sSqlceCheckDataContext.sqlceCheckDataContext_connectionString,
                    z4sSqlceCheckDataContext.sqlceCheckDataContext_id, zen4SyncSymmetricKeyPassword );
            }
            else
            {
                sqlceCDC.sqlceCheckDataContext_connectionString = Encoding.UTF8.GetString( z4sSqlceCheckDataContext.sqlceCheckDataContext_connectionString );
            }

            return sqlceCDC;

        }

        private SqlServerCheckDataContext POCOsqlServerCheckDataContext( Guid sqlServerCheckDataContextId, string zen4SyncSymmetricKeyPassword )
        {
            sqlServerCheckDataContext z4sSqlServerCheckDataContext = z4sEntityProvider.GetSqlServerCheckDataContext( sqlServerCheckDataContextId );
            SqlServerCheckDataContext sqlServerCDC =  new SqlServerCheckDataContext()
            {
                sqlServerCheckDataContext_id = z4sSqlServerCheckDataContext.sqlServerCheckDataContext_id,
                sqlServerCheckDataContext_parameters = ConvertToPOCO( z4sSqlServerCheckDataContext.sqlServerCheckDataParameter ),

            };

            if(z4sSqlServerCheckDataContext.sqlServerCheckDataContext_isEncryptedConnectionString)
            {
                sqlServerCDC.sqlServerCheckDataContext_connectionString = z4sEntityProvider.DecryptData( z4sSqlServerCheckDataContext.sqlServerCheckDataContext_connectionString,
                z4sSqlServerCheckDataContext.sqlServerCheckDataContext_id, zen4SyncSymmetricKeyPassword );
            }
            else
            {
                sqlServerCDC.sqlServerCheckDataContext_connectionString = Encoding.UTF8.GetString( z4sSqlServerCheckDataContext.sqlServerCheckDataContext_connectionString );
            }
            return sqlServerCDC;                            
        }

        private SqlceExecutionContext POCOsqlCeExecutionContext( Guid sqlCeExecutionContextId, string zen4SyncSymmetricKeyPassword )
        {
            sqlceExecutionContext z4sSqlCeExecutionContext = z4sEntityProvider.GetSqlceExecutionContext( sqlCeExecutionContextId );
            SqlceExecutionContext sqlceEC = new SqlceExecutionContext()
            {
                sqlceExecutionContext_id = z4sSqlCeExecutionContext.sqlceExecutionContext_id,
                sqlceExecutionContext_parameters = ConvertToPOCO( z4sSqlCeExecutionContext.sqlceExecutionParameter )
            };

            if(z4sSqlCeExecutionContext.sqlceExecutionContext_isEncryptedConnectionString)
            {
                sqlceEC.sqlCeExecutionContext_connectionString = z4sEntityProvider.DecryptData( z4sSqlCeExecutionContext.sqlceExecutionContext_connectionString,
                z4sSqlCeExecutionContext.sqlceExecutionContext_id, zen4SyncSymmetricKeyPassword );
            }
            else
            {
                sqlceEC.sqlCeExecutionContext_connectionString = Encoding.UTF8.GetString( z4sSqlCeExecutionContext.sqlceExecutionContext_connectionString );
            }

            return sqlceEC;                            
        }

        private SqlServerExecutionContext POCOsqlServerExecutionContext( Guid sqlServerExecutionContextId, string zen4SyncSymmetricKeyPassword )
        {
            sqlServerExecutionContext z4sSqlServerExecutionContext = z4sEntityProvider.GetSqlServerExecutionContext( sqlServerExecutionContextId );
            SqlServerExecutionContext sqlServerEC = new SqlServerExecutionContext()
            {
                sqlServerExecutionContext_id = z4sSqlServerExecutionContext.sqlServerExecutionContext_id,
                sqlServerExecutionContext_parameters = ConvertToPOCO( z4sSqlServerExecutionContext.sqlServerExecutionParameter ),
            };
            if(z4sSqlServerExecutionContext.sqlServerExecutionContext_isEncryptedConnectionString)
            {
                sqlServerEC.sqlServerExecutionContext_connectionString = z4sEntityProvider.DecryptData( z4sSqlServerExecutionContext.sqlServerExecutionContext_connectionString,
                z4sSqlServerExecutionContext.sqlServerExecutionContext_id, zen4SyncSymmetricKeyPassword );
            }
            else
            {
                sqlServerEC.sqlServerExecutionContext_connectionString = Encoding.UTF8.GetString( z4sSqlServerExecutionContext.sqlServerExecutionContext_connectionString );
            }
            return sqlServerEC;                    
        }
        
        private List<SqlceExecutionParameter> ConvertToPOCO( EntityCollection<sqlceExecutionParameter> z4sSqlceExecutionParameters )
        {
            List<SqlceExecutionParameter> sqlceExecutionParameters = new List<SqlceExecutionParameter>();
            foreach ( sqlceExecutionParameter z4sSqlceEP in z4sSqlceExecutionParameters )
            {
                SqlceExecutionParameter sqlceEP = new SqlceExecutionParameter()
                {
                    sqlceExecutionParameter_name = z4sSqlceEP.sqlceExecutionParameter_name,
                    sqlceExecutionParameter_value = z4sSqlceEP.sqlceExecutionParameter_value,
                    sqlceExecutionParameter_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlceEP.sqlceExecutionParameter_sqlValueTypeId)
                };
                sqlceExecutionParameters.Add( sqlceEP );
            }
            return sqlceExecutionParameters;
        }

        private List<SqlServerExecutionParameter> ConvertToPOCO( EntityCollection<sqlServerExecutionParameter> z4sSqlServerExecutionParameters )
        {
            List<SqlServerExecutionParameter> sqlServerExecutionParameters = new List<SqlServerExecutionParameter>();
            foreach ( sqlServerExecutionParameter z4sSqlSEP in z4sSqlServerExecutionParameters )
            {
                SqlServerExecutionParameter sqlSEP = new SqlServerExecutionParameter()
                {
                     sqlServerExecutionParameter_name = z4sSqlSEP.sqlServerExecutionParameter_name,
                     sqlServerExecutionParameter_value = z4sSqlSEP.sqlServerExecutionParameter_value,
                     sqlServerExecutionParameter_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlSEP.sqlServerExecutionParameter_sqlValueTypeId)
                };
                sqlServerExecutionParameters.Add( sqlSEP );
            }
            return sqlServerExecutionParameters;
        }

        private List<SqlceCheckDataParameter> ConvertToPOCO( EntityCollection<sqlceCheckDataParameter> z4sSqlceCheckDataParameters )
        {
            List<SqlceCheckDataParameter> sqlceCheckDataParameters = new List<SqlceCheckDataParameter>();
            foreach ( sqlceCheckDataParameter z4sSqlceCDP in z4sSqlceCheckDataParameters )
            {
                SqlceCheckDataParameter sqlceCDP = new SqlceCheckDataParameter()
                {
                    sqlceCheckDataParameter_name = z4sSqlceCDP.sqlceCheckDataParameter_name,
                    sqlceCheckDataParameter_value = z4sSqlceCDP.sqlceCheckDataParameter_value,
                    sqlceCheckDataParameter_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlceCDP.sqlceCheckDataParameter_sqlValueTypeId)
                };
                sqlceCheckDataParameters.Add( sqlceCDP );
            }
            return sqlceCheckDataParameters;
        }

        private List<SqlServerCheckDataParameter> ConvertToPOCO( EntityCollection<sqlServerCheckDataParameter> z4sSqlServerCheckDataParameters )
        {
            List<SqlServerCheckDataParameter> sqlServerCheckDataParameters = new List<SqlServerCheckDataParameter>();
            foreach ( sqlServerCheckDataParameter z4sSqlSCDP in z4sSqlServerCheckDataParameters )
            {
                SqlServerCheckDataParameter sqlSCDP = new SqlServerCheckDataParameter()
                {
                    sqlServerCheckDataParameter_name = z4sSqlSCDP.sqlServerCheckDataParameter_name,
                    sqlServerCheckDataParameter_value = z4sSqlSCDP.sqlServerCheckDataParameter_value,
                    sqlServerCheckDataParameter_sqlValueTypeEnum = GuidToEnum.SqlValueType(z4sSqlSCDP.sqlServerCheckDataParameter_sqlValueTypeId)
                };

                sqlServerCheckDataParameters.Add( sqlSCDP );
            }
            return sqlServerCheckDataParameters;
        }

        private Subscriber ConvertToPOCO( subscriber z4sSubscriber )
        {
            Subscriber subscriber = new Subscriber()
            {
                subscriber_id = z4sSubscriber.subscriber_id,
                subscriber_name = z4sSubscriber.subscriber_name,
                subscriber_hostName = z4sSubscriber.subscriber_hostName,
                subscriber_connectionString = z4sSubscriber.subscriber_connectionString,
                subscriber_deleteSdfIfItExists = z4sSubscriber.subscriber_deleteSdfIfItExists,
                subscriber_sdfFileName = z4sSubscriber.subscriber_sdfFileName,
                subscriber_sdfFileDirectory = z4sSubscriber.subscriber_sdfFileDirectory,
                subscriber_validateTypeEnum = GuidToEnum.ValidateType(z4sSubscriber.subscriber_validateTypeId),
                subscriber_reinitializeSubscription = z4sSubscriber.subscriber_reinitializeSubscription,
                subscriber_uploadBeforeReinitialize = z4sSubscriber.subscriber_uploadBeforeReinitialize
            };

            return subscriber;
        }

        /// <summary>
        /// Returns all the POCO Activities
        /// </summary>
        /// <param name="z4sActivities">A list of Zen4Sync activities</param>
        /// <param name="TestScenario">The TestScenario corresponding to the list of activities</param>
        /// <returns>A list of POCO activities corresponding to a TestScenario</returns>
        private List<Activity> ConvertToPOCO( EntityCollection<activity> z4sActivities, string zen4SyncSymmetricKeyPassword )
        {
            List<Activity> activities = new List<Activity>();
            foreach ( activity z4sActivity in z4sActivities )
            {
                Activity activity = ConvertToPOCO( z4sActivity, zen4SyncSymmetricKeyPassword );
                activities.Add( activity );
            }
            return activities;
        }

        /// <summary>
        /// Converts a Zen4Sync publisher into a POCO Publisher
        /// </summary>
        /// <param name="z4sPublisher">Zen4Sync publisher</param>
        /// <returns>A POCO Publisher</returns>
        private Publisher ConvertToPOCO( publisher z4sPublisher, string zen4SyncSymmetricKeyPassword )
        {
            Publisher publisher = new Publisher()
            {
                publisher_id = z4sPublisher.publisher_id,
                publisher_name = z4sPublisher.publisher_name,
                publisher_address = z4sPublisher.publisher_address,
                publisher_database = z4sPublisher.publisher_database,
                publisher_login = z4sPublisher.publisher_login,
                publisher_password = z4sEntityProvider.DecryptData( z4sPublisher.publisher_password, z4sPublisher.publisher_id, zen4SyncSymmetricKeyPassword ),
                publisher_networkTypeEnum = GuidToEnum.NetworkType(z4sPublisher.publisher_networkTypeId),
                publisher_securityTypeEnum = GuidToEnum.SecurityType(z4sPublisher.publisher_securityTypeId)
            };

            return publisher;
        }

        /// <summary>
        /// Converts a Zen4Sync distributor into a POCO Distributor
        /// </summary>
        /// <param name="z4sDistributor">Zen4Sync distributor</param>
        /// <returns>A POCO Distributor</returns>
        private Distributor ConvertToPOCO( distributor z4sDistributor, string zen4SyncSymmetricKeyPassword )
        {
            Distributor distributor = new Distributor()
            {
                 distributor_id = z4sDistributor.distributor_id,
                 distributor_name = z4sDistributor.distributor_name,
                 distributor_address = z4sDistributor.distributor_address,
                 distributor_login = z4sDistributor.distributor_login,
                 distributor_profileName = z4sDistributor.distributor_profileName,
                 distributor_password = z4sEntityProvider.DecryptData( z4sDistributor.distributor_password, z4sDistributor.distributor_id, zen4SyncSymmetricKeyPassword ),
                 distributor_networkTypeEnum = GuidToEnum.NetworkType(z4sDistributor.distributor_networkTypeId),
                 distributor_securityTypeEnum = GuidToEnum.SecurityType(z4sDistributor.distributor_securityTypeId)
            };

            return distributor;
        }

        /// <summary>
        /// Converts a Zen4Sync internet into a POCO Internet
        /// </summary>
        /// <param name="z4sInternet">Zen4Sync internet</param>
        /// <returns>A POCO Internet</returns>
        private Internet ConvertToPOCO( internet z4sInternet, string zen4SyncSymmetricKeyPassword )
        {
            return new Internet()
            {
                internet_id = z4sInternet.internet_id,
                internet_name = z4sInternet.internet_name,
                internet_url = z4sInternet.internet_url,
                internet_login = z4sInternet.internet_login,             
                internet_proxyServer = z4sInternet.internet_proxyServer,
                internet_proxyLogin = z4sInternet.internet_proxyLogin,
                internet_password =z4sEntityProvider.DecryptData(z4sInternet.internet_password, z4sInternet.internet_id,zen4SyncSymmetricKeyPassword),
                internet_proxyPassword = z4sEntityProvider.DecryptData(z4sInternet.internet_proxyPassword, z4sInternet.internet_id, zen4SyncSymmetricKeyPassword)
            };

        }

        /// <summary>
        /// Converts a POCO SqlceExecutionContext into a Zen4Sync sqlceExecutionContext
        /// </summary>
        /// <param name="sqlceExecutionContextPOCO">POCO SqlceExecutionContext</param>
        /// <returns>a Zen4Sync sqlceExecutionContext</returns>
        internal sqlceExecutionContext ConvertToEntity( SqlceExecutionContext sqlceExecutionContextPOCO )
        {
            EntityCollection<sqlceExecutionParameter> sqlceExecutionParameters = new EntityCollection<sqlceExecutionParameter>();
            foreach ( SqlceExecutionParameter sqlceExecutionParameterPOCO in sqlceExecutionContextPOCO.sqlceExecutionContext_parameters )
            {
                sqlceExecutionParameter sqlceExecutionParameter = new sqlceExecutionParameter()
                {
                    sqlceExecutionParameter_name = sqlceExecutionParameterPOCO.sqlceExecutionParameter_name,
                    sqlceExecutionParameter_value = sqlceExecutionParameterPOCO.sqlceExecutionParameter_value
                };
                if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.UniqueIdentifier )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.UniqueIdentifier;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Int )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Int;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Nvarchar )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.NVarchar;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Bit )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Bit;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.DateTime )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.DateTime;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Xml )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Xml;
                }
                else if ( sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.SmallInt )
                {
                    sqlceExecutionParameter.sqlceExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.SmallInt;
                }
                else
                {
                    throw new Zen4SyncOrchestratorException( Zen4SyncOrchestratorException.Messages.UNKNOWN_ITEM_BY_ID, new string[] { "sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum", sqlceExecutionParameterPOCO.sqlceExecutionParameter_sqlValueTypeEnum.ToString() } );
                }
                sqlceExecutionParameters.Add( sqlceExecutionParameter );
            }

            return new sqlceExecutionContext()
            {
                sqlceExecutionContext_id = sqlceExecutionContextPOCO.sqlceExecutionContext_id,
                sqlceExecutionParameter = sqlceExecutionParameters,
            };
        }

        /// <summary>
        /// Converts a POCO SqlServerExecutionContext into a Zen4Sync sqlServerExecutionContext
        /// </summary>
        /// <param name="sqlServerExecutionContextPOCO">POCO SqlServerExecutionContext</param>
        /// <returns>a Zen4Sync sqlServerExecutionContext</returns>
        internal sqlServerExecutionContext ConvertToEntity( SqlServerExecutionContext sqlServerExecutionContextPOCO )
        {
            EntityCollection<sqlServerExecutionParameter> sqlServerExecutionParameters = new EntityCollection<sqlServerExecutionParameter>();
            foreach ( SqlServerExecutionParameter sqlServerExecutionParameterPOCO in sqlServerExecutionContextPOCO.sqlServerExecutionContext_parameters )
            {
                sqlServerExecutionParameter sqlServerExecutionParameter = new sqlServerExecutionParameter()
                {
                    sqlServerExecutionParameter_name = sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_name,
                    sqlServerExecutionParameter_value = sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_value
                };
                if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.UniqueIdentifier )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.UniqueIdentifier;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Int )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Int;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Nvarchar )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.NVarchar;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Bit )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Bit;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.DateTime )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.DateTime;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.Xml )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.Xml;
                }
                else if ( sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum == SqlValueTypeEnum.SmallInt )
                {
                    sqlServerExecutionParameter.sqlServerExecutionParameter_sqlValueTypeId = Zen4SyncModel.Referential.Guid.SqlValueType.SmallInt;
                }
                else
                {
                    throw new Zen4SyncOrchestratorException( Zen4SyncOrchestratorException.Messages.UNKNOWN_ITEM_BY_ID, new string[] { "sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum", sqlServerExecutionParameterPOCO.sqlServerExecutionParameter_sqlValueTypeEnum.ToString() } );
                }
                sqlServerExecutionParameters.Add( sqlServerExecutionParameter );
            }

            return new sqlServerExecutionContext()
            {
                sqlServerExecutionContext_id = sqlServerExecutionContextPOCO.sqlServerExecutionContext_id,
                sqlServerExecutionParameter = sqlServerExecutionParameters
            };
        }

        /// <summary>
        /// Converts a Zen4Sync testServer into a POCO TestServer
        /// </summary>
        /// <param name="z4sTestServer"> Zen4Sync testServer</param>
        /// <returns>The POCO TestServer of the corresponding Zen4Sync testServer</returns>
        internal TestServer ConvertToPOCO( testServer z4sTestServer )
        {
            return new TestServer()
            {
                testServer_id = z4sTestServer.testServer_id,
                testServer_hostName = z4sTestServer.testServer_hostName,
                testServer_wcfConfigSectionName = z4sTestServer.testServer_wcfConfigSectionName,
                testServer_loadSyncApplicationPath = z4sTestServer.testServer_loadSyncApplicationPath,
                testServer_configFilesDirectory = z4sTestServer.testServer_configFilesDirectory
            };
        }
    }
}




